<?

class Arr
{
    static function arraySimplify(&$arr)
    {
        if (!is_array($arr)) return;
        if (count($arr) === 1) {
            $arr = reset($arr);
            self::arraySimplify($arr);
        }
        if (count($arr) > 1) 
        	foreach ($arr as $key => $val) self::arraySimplify($arr[$key]);
        if (empty($arr)) $arr = false;
    }
    
    static public function keepFeilds($arr, $keys)
    {
        if (!is_array($arr)) return false;
        $ret = array();
        if (self::isArrayOfArrays($arr)) {
            foreach ($arr as $one) {
                if (is_array($one))
                    $ret[] = self::keepFeilds($one, $keys);
                else
                    $ret[] = $one;
            }
        }
        else {
            $tmp_arr = array_combine($keys, $keys);
            $ret = array_intersect_key($arr, $tmp_arr);
            foreach ($keys as $key => $val) {
                if (is_numeric($key)) continue;
                if ($key === $val) continue;
                $tmp = $ret[$val];
                unset($ret[$val]);
                $ret[$key] = $tmp;
            }
        }
        return $ret;
    }

    static public function isArrayOfArrays($arr)
    {
        if (!is_array($arr)) return false;
        if (!count($arr)) return false;
        if (!is_array(reset($arr))) return false;
        if (!is_array(end($arr))) return false;
        return true;
    }
    
    static public function object2array($obj){
        $arr = (array)$obj;
        foreach($arr as $key=>$val){
            if(is_object($val) || is_array($val))
                $arr[$key] = self::object2array($arr[$key]);
        }
        return $arr;
    }
    
    static public function iconv($charset_in, $charset_out, &$arr){
        if (is_string($arr)){ 
            $arr = iconv($charset_in, $charset_out, $arr);
            return;
        }
        if (!is_array($arr)) return;
        if (!count($arr)) return;
        foreach ($arr as $key => $val) self::iconv($charset_in, $charset_out, $arr[$key]);
    }
    
    static public function rotate($arr){
        if(!self::isArrayOfArrays($arr)) return false;
        $ret = array();
        foreach($arr as $key1 => $row){
            foreach($row as $key2 => $val){
                $ret[$key2][$key1] = $val;
            }
        }
        return $ret;
    }
}

?>